package cn.tedu.boot11.controller;

import cn.tedu.boot11.entity.Product;
import cn.tedu.boot11.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController

public class ProductController {
    @Autowired(required = false)
    ProductMapper mapper;

    @RequestMapping("/insert")
    public String insert(Product product) {
        System.out.println("product = " + product);
        mapper.insert(product);

        return "添加完成";
    }

    @RequestMapping("/select")
    public String select() {
        List<Product> list = mapper.select();
        String html = "<table border = 1 </table>";
        html += "<tr>";
        html += "<td>商品序号</td><td>商品标题</td><td>商品价格</td><td>商品销量</td>";
        html += "</tr>";
        for (Product p : list) {
            html += "<tr>";
            html+="<td>"+p.getId()+"</td>";
            html+="<td>"+p.getTitle()+"</td>";
            html+="<td>"+p.getPrice()+"</td>";
            html+="<td align= center>"+p.getSaleCount()+"</td>";
            html+="<td> <a href='/delete?id="+p.getId()+"'> 删除</a> </td>";
            html += "</tr>";
        }
        html += "</table>";
        return html;
    }
    @RequestMapping("/delete")
    public  String delete(int id){
        System.out.println("id = " + id);
        mapper.delete(id);
        return  "删除成功";
    }
    @RequestMapping("/update")
    public  String update(Product product){
        System.out.println("product = " + product);
        mapper.update(product);
        return "修改成功";
    }
}
